package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.SigninDetailsEntity;
import com.tfjybj.itoo.exam.model.SigninDetailsModel;
import com.tfjybj.itoo.exam.provider.service.SigninDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * SigninDetailsController
 * signinDetails表
 *
 * @author 郭满亮
 * @version 2.0.0
 * @since 2.0.0 2019-03-19 09:21:41
 */
@Api(tags = {"signinDetails小练签到详情表接口"})
@RequestMapping(value = "/signinDetails")
@RestController
public class SigninDetailsController {

    @Resource
    private SigninDetailsService signinDetailsService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model SigninDetailsModel
     * @return 添加的结果
     * @author 郭满亮
     * @since 2.0.0 2019-03-19 09:21:41
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody SigninDetailsModel model) {
        SigninDetailsEntity signinDetailsEntity = new SigninDetailsEntity();
        BeanUtils.copyProperties(model, signinDetailsEntity);
        signinDetailsService.save(signinDetailsEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 郭满亮
     * @since 2.0.0 2019-03-19 09:21:41
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        signinDetailsService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 郭满亮
     * @since 2.0.0 2019-03-19 09:21:41
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        signinDetailsService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model SigninDetailsModel
     * @return 修改后的结果
     * @author 郭满亮
     * @since 2.0.0 2019-03-19 09:21:41
     */
    @ApiOperation(value = "根据id修改signinDetails")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody SigninDetailsModel model) {
        SigninDetailsEntity signinDetailsEntity = new SigninDetailsEntity();
        BeanUtils.copyProperties(model, signinDetailsEntity);
        signinDetailsService.updateById(signinDetailsEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找SigninDetails
     *
     * @param id 主键id
     * @return 根据id查找的结果
     * @author 郭满亮
     * @since 2.0.0 2019-03-19 09:21:41
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        SigninDetailsEntity signinDetailsEntity = signinDetailsService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", signinDetailsEntity);
    }

    /**
     * 分页查询所有SigninDetails
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 郭满亮
     * @since 2.0.0 2019-03-19 09:21:41
     */
    @ApiOperation(value = "分页查询所有SigninDetails")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<SigninDetailsEntity> signinDetailss = signinDetailsService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", signinDetailss);
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据签到表id查询所有学生签到情况
     * @param signInId 签到表id
     * @return 所有学生签到情况
     * @author 郭满亮
     * @since  2019年3月20日10:08:00
     */
    @ApiOperation(value = "根据签到表id查询所有学生签到情况")
    @PostMapping(value = {"/queryAllSigninDetails/{signInId}"})
    public ItooResult queryAllSigninDetails(@ApiParam(value = "签到表id",required = true) @PathVariable String signInId){

        if (signInId.isEmpty() || signInId == null) {
            return ItooResult.build(ItooResult.FAIL,"查询失败,签到表ID为空");
        }
        return signinDetailsService.queryAllSigninDetails(signInId);
    }

    /**
     * 根据上课班id查询radis所有学生签到状态
     * @param teachClassId  上课班id
     * @return  查询结果
     * @author 郭满亮
     * @since 2019年3月20日11:34:36
     */
    @ApiOperation(value = "根据上课班id查询radis所有学生签到状态")
    @PostMapping(value = {"/queryRadisStudentState/{teachClassId}"})
    public ItooResult queryRadisStudentState(@ApiParam(value = "上课班id",required = true) @PathVariable String teachClassId) {
        if (teachClassId == null || teachClassId.isEmpty()){
            return ItooResult.build(ItooResult.FAIL,"查询失败,上课班id为空");
        }
        return signinDetailsService.queryRadisStudentState(teachClassId);
    }
    /**
     * 查询学生所在的上课班
     * @author 于云秀
     * @since 2019年3月20日
     * @param studentId
     * @return signinDetailss
     */
    @ApiOperation(value = "查询学生所在的上课班")
    @GetMapping(value = {"/selectTeacherClass/{studentId}"})
    public ItooResult selectTeacherClass(@ApiParam(name = "studentId",value = "学生id",required = true)@PathVariable String studentId) {
        List<SigninDetailsEntity> signinDetailss = signinDetailsService.selectTeacherClass(studentId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", signinDetailss);
    }

    /**
     * 签到更新学生状态
     * @author 于云秀
     * @since 2019年3月20日
     * @param courseId
     * @param state
     * @return signinDetailss
     */
    @ApiOperation(value = "签到更新学生状态")
    @GetMapping(value = {"/updateStudentState/{courseId}/{state}/{studentId}"})
    public ItooResult updateStudentState(@ApiParam(name = "courseId", value = "上课班id", required = true) @PathVariable String courseId,
                                         @ApiParam(name = "state", value = "考生状态", required = true) @PathVariable String state,
                                         @ApiParam(name = "studentId", value = "学生id", required = true) @PathVariable String studentId) {
        if (courseId == null || courseId.isEmpty()){
            return ItooResult.build(ItooResult.FAIL, "上课班ID为空");
        }
        if (state == null || state.isEmpty()){
            return ItooResult.build(ItooResult.FAIL, "请填写签到状态");
        }
        if (studentId == null || studentId.isEmpty()){
            return ItooResult.build(ItooResult.FAIL, "学生ID为空");
        }
//        List<SigninDetailsEntity> selectState = signinDetailsService.selectState(courseId, studentId);
//        if (selectState.size() < 1) {

            int flag = signinDetailsService.updateStudentState(courseId, studentId, state);
            if (flag>0) {
                return ItooResult.build(ItooResult.SUCCESS, "签到成功");
            }
            return ItooResult.build(ItooResult.FAIL, "签到未开始");
//        }
//        return ItooResult.build(ItooResult.FAIL, "您已签到");
    }

    /**
     * 查询学生是否签到
     * @author 于云秀
     * @since 2019年3月26日
     * @param courseId
     * @param studentId
     * @return
     */
    @ApiOperation(value = "查询学生是否签到")
    @GetMapping(value = {"/selectState/{courseId}/{studentId}"})
    public ItooResult selectState(@ApiParam(name = "courseId", value = "上课班id", required = true) @PathVariable String courseId,
                                         @ApiParam(name = "studentId", value = "学生id", required = true) @PathVariable String studentId) {
        List<SigninDetailsEntity> selectState = signinDetailsService.selectState(courseId, studentId);
        if (selectState.size() < 1) {
            return ItooResult.build(ItooResult.SUCCESS, "未签到");
       }
       return ItooResult.build(ItooResult.FAIL, "已签到");
    }
}    
